-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
26 lines (21 loc) · 877 Bytes
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def generate_subsets(s, index, current_subset):
# Base case: if we've processed all elements, print the current subset.
if index == len(s):
print("{", " ".join(map(str, current_subset)), "}")
return
# Recurse without including the current element.
generate_subsets(s, index + 1, current_subset)
# Include the current element.
current_subset.append(s[index])
generate_subsets(s, index + 1, current_subset)
# Backtrack: remove the last element before returning.
current_subset.pop()
if __name__ == "__main__":
n = int(input("Enter the number of elements in the set: "))
s = []
print("Enter the elements:")
for _ in range(n):
# Change int(input()) to input() if you want to work with strings.
s.append(int(input()))
print("\nAll subsets:")
generate_subsets(s, 0, [])